<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>教师列表页</title>
<!-- 下载并引入jquery easyUI -->
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/jquery-easyui-1.8.8/themes/gray/easyui.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/jquery-easyui-1.8.8/themes/icon.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/jquery-easyui-1.8.8/themes/color.css">
<script type="text/javascript"
	src="${pageContext.request.contextPath}/jquery-easyui-1.8.8/jquery.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/jquery-easyui-1.8.8/jquery.easyui.min.js"></script>


</head>
<body>
	<table id="teacher_dg" title="教师列表" class="easyui-datagrid"
		style="width: 1150px; height: 480px"
		url="${param.request.contextPath}/admin/teacherList"
		toolbar="#teacher_toolbar" pagination="true" rownumbers="true"
		fitColumns="true" singleSelect="true">
		<thead>
			<tr>
				<th field="cb" checkbox="true" align="center"></th>
				<th field="tehId" width="50">教师编号</th>
				<th field="name" width="50">教师名称</th>
				<th field="sex" formatter="sexFormatter" width="50">性别</th>
				<th field="age" width="50">年龄</th>
				<th field="place" width="50">籍贯</th>
				<th field="homeAdd" width="50">家庭住址</th>
				<th field="tel" width="50">电话号码</th>
				<th field="idNumber" width="50">身份证号码</th>
				<th field="jobTime" width="50">入职日期</th>
				<th field="gradeName" formatter="gradeFormatter" width="50">教师等级</th>
			</tr>
		</thead>
	</table>

	<!-- 工具条组件 -->
	<div id="teacher_toolbar">
		<a href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-add" plain="true" onclick="newTeacher()">新增</a> <a
			href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-edit" plain="true" onclick="editTeacher()">修改</a> <a
			href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-remove" plain="true" onclick="destroyTeacher()">删除</a>
	</div>

	<!-- 新增和修改表单  start -->
	<div id="teacher_dlg" class="easyui-dialog"
		style="width: 400px; height: 280px; padding: 10px 20px" closed="true"
		buttons="#teacher_dlg-buttons">
		<div class="ftitle">教师信息管理</div>
		<form id="teacher_fm" enctype="multipart/form-data" method="post">
			<div class="fitem">
				<label>教师编号:</label> <input name="tehId"
					class="easyui-validatebox" required="true">
			</div>
			<div class="fitem">
				<label>教师名称:</label> <input name="name"
					class="easyui-validatebox" data-options="required:true, missingMessage:'请输入教师名称'">
			</div>
			<div class="fitem">
				<label>性　　别:</label>
				<input name="sex" id="sex">
			</div>
			<div class="fitem">
				<label>年　　龄:</label> <input name="age"
					class="easyui-numberspinner" data-options="min:18,max:60,precision:0,required:true,editable:true">
			</div>
			<div class="fitem">
				<label>籍　　贯:</label> <input name="place"
					class="easyui-validatebox" data-options="required:true, missingMessage:'请输入籍贯'">
			</div>
			<div class="fitem">
				<label>家庭地址:</label> <input name="homeAdd"
					class="easyui-validatebox" data-options="required:true, missingMessage:'请输入家庭地址'">
			</div>
			<div class="fitem">
				<label>电话号码:</label> <input name="tel"
					class="easyui-validatebox" data-options="required:true, missingMessage:'请输入电话号码',validType:'mobile'">
			</div>
			<div class="fitem">
				<label>身份证号码:</label> <input name="idNumber" class="easyui-validatebox" 
				 data-options="required:true, validType:'idCode'">
			</div>
			<div class="fitem">
				<label>入职日期:</label> <input name="jobTime"
					class="easyui-datebox" required="true">
			</div>
			<div class="fitem">
				<label>教师等级:</label> <input id="tehGrade" name="tehGrade.tgId" class="easyui-combobox" 
				 data-options="valueField:'tgId',textField:'gradeName',editable:false,
					url:'${param.request.contextPath}/tehGrade/listall'" required="true">
			</div>
		</form>
	</div>
	<div id="teacher_dlg-buttons">
		<a href="#" class="easyui-linkbutton" iconCls="icon-ok"
			onclick="saveTeacher()">保存</a> <a href="#" class="easyui-linkbutton"
			iconCls="icon-cancel"
			onclick="javascript:$('#teacher_dlg').dialog('close')">取消</a>
	</div>

	<!-- 新增和修改表单 end -->

	<script type="text/javascript">
		$.extend($.fn.validatebox.defaults.rules, {
			idCode:{
		          validator:function(value,param){
		            return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(value);
		          },
		          message: '请输入正确的身份证号'
		        },
		        mobile: {
		            validator: function (value, param) {
		              return /^(?:13\d|15\d|18\d)-?\d{5}(\d{3}|\*{3})$/.test(value);
		            },
		            message: '手机号码不正确'
		          }
		});
		$('#sex').combobox({
			 data:[
				 {
					  "sexId": 0,
					  "sexName": "女"
					}
					,{
					  "sexId": 1,
					  "sexName": "男"
					}
					],
	        valueField:'sexId',
	        textField:'sexName',
	        required:'true',
	        editable:false
	        });
		function sexFormatter(value, row, index) {
			if (value == 1) {
				return '男';
			}
			return '女';
		}
		function gradeFormatter(value, row, index) {
			if(row.tehGrade == null){
				return null;
			}
			return row.tehGrade.gradeName;
		}
		//下拉列表反选的方法
		function loadCategorySuccess(tgId) {
			var data = $("#tehGrade").combobox("getData");
			if (data && data.length > 0) {
				if (tgId == 0) {
					$("#tehGrade").combobox("setValue", data[0]['tgId']);
				} else {
					$("#tehGrade").combobox("setValue", tgId);
				}
			}
		}

		// 新增
		function newTeacher() {
			$('#teacher_dlg').dialog('open').dialog('setTitle', '新增教师信息');
			$('#teacher_fm').form('clear');
			url = '${pageContext.request.contextPath}/teacher/add';
			console.log($("#sex").combobox("getData")[0]['sexId']);
			$("#sex").combobox("setValue",$("#sex").combobox("getData")[0]['sexId']);
			loadCategorySuccess(0);
		}

		// 修改
		function editTeacher() {
			$('#teacher_fm').form('clear');
			var row = $('#teacher_dg').datagrid('getSelected');
			if (row) {
				$('#teacher_dlg').dialog('open').dialog('setTitle', '修改教师信息');
				$('#teacher_fm').form('load', row);
				url = '${pageContext.request.contextPath}/teacher/edit?id='
						+ row.id;
				loadCategorySuccess(row.tehGrade.tgId);
			}
		}

		function saveTeacher() {
			$('#teacher_fm').form('submit', {
				url : url,
				onSubmit : function() {
					return $(this).form('validate');
				},
				success : function(result) {
					var result = eval('(' + result + ')');
					if (!result.success) {
						$.messager.show({
							title : 'Error',
							msg : result.errorMsg
						});
					} else {
						$.messager.show({
							title : 'Info',
							msg : result.errorMsg
						});
					}

					$('#teacher_dlg').dialog('close'); // close the dialog
					$('#teacher_dg').datagrid('reload'); // reload the user data
				}
			});
		}

		function destroyTeacher() {
			var row = $('#teacher_dg').datagrid('getSelected');
			if (row) {
				$.messager
						.confirm(
								'Confirm',
								'确认删除教师信息?',
								function(r) {
									if (r) {
										$
												.post(
														'${pageContext.request.contextPath}/teacher/delete',
														{
															id : row.id
														},
														function(result) {
															if (result.success) {
																$('#teacher_dg')
																		.datagrid(
																				'reload'); // reload the user data
															} else {
																$.messager
																		.show({ // show error message
																			title : 'Error',
																			msg : result.errorMsg
																		});
															}
														}, 'json');
									}
								});
			}
		}
	</script>
</body>
</html>